package com.pet.server.ext.action.android.home;


import java.util.ArrayList;
import java.util.List;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.business.Business;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Campaign;
import com.pet.server.ext.data.Faxian;
import com.pet.server.ext.data.PetAdopt;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.service.ArticleService;
import com.pet.server.ext.service.PetService;
import com.pet.server.ext.service.ProductService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.RandomCollection;
import com.pet.server.ext.util.cache.ConstantUtil;
import com.pet.server.ext.util.cache.PetCache;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Mall extends ActionBase{
	public Integer article_id;
	public String wechat="0";
	public Mall(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		
	}
	
	@Override
	public Result invokeService() throws ApiError {

		Result result = new Result();
		UserService userService = new UserService();
		ArticleService articleService = new ArticleService();
		Business business = new Business();
		ProductService productService = new ProductService();
		PetService petService = new PetService();
		
		
        //banner
		List<Campaign> banners = userService.queryForHome(BaseAttribute.pay_type.banner.toString(),6);
		
		
		JSONArray request = new JSONArray();
		//tiwen
		List<RequestInfo> requestList = userService.queryRequestForHome(10);
		List<RequestInfo> requests = new ArrayList<>();
		RandomCollection<RequestInfo>  random = new RandomCollection<>();
		for(RequestInfo r:requestList){
			random.add(5, r);
		}
		int i=3;
		RequestInfo req=random.removeNext();
		while(null!=req&&i>0){
			requests.add(req);
			i--;
			req=random.removeNext();
		}
		
		//文章
		List<Article> articles = articleService.queryArticleForIndex(null, null, null);
		//商品
		List<ProductInfo> list = productService.queryProductInfo(8);
		
		List<PetAdopt> adopt = petService.queryPetAdopt(5);
		
		
	    JSONObject data = business.homeDating(banners, articles, articles, adopt, requests, list, user);
		
	    
		result.put("data", data);
		return result;
	
	}


}
